import Vue from 'vue'
import VueRouter from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import Films from '@/views/myFilms'
import Nowplaying from '@/views/films/myNowplaying'
import Comingsoon from '@/views/films/myComingsoon'
import Detail from '@/views/myDetail'
import Cinemas from '@/views/myCinemas'
import City from '@/views/myCity'
import Search from '@/views/mySearch'
// import Center from '@/views/Center'
// import Order from '@/views/Order'
import Login from '@/views/myLogin'

Vue.use(VueRouter) // 注册路由插件

const routes = [
  {
    path: '/films',
    component: Films,
    children: [
      {
        path: '/films/nowplaying',
        component: Nowplaying
      },
      {
        path: '/films/comingsoon',
        component: Comingsoon
      },
      {
        path: '/films',
        redirect: '/films/nowplaying'
      }
    ]
  },
  {
    name: 'kerwinDetail', // 命名路由
    path: '/detail/:id', // 动态路由
    component: Detail
  },
  {
    path: '/cinemas',
    component: Cinemas
  },
  {
    path: '/city',
    component: City
  },
  {
    path: '/cinemas/search',
    component: Search
  },
  {
    path: '/center',
    component: () => import('@/views/myCenter'),
    meta: {
      isKerwinRequired: true
    }
    // beforeEnter: (to, from, next) => {
    //   if (localStorage.getItem('token')) {
    //     next ()
    //   } else {
    //     next ({
    //       path: '/login',
    //       query: {aaa: to.fullPath}
    //     })
    //   }
    // }
  },
  {
    path: '/order',
    component: () => import('@/views/myOrder'),
    meta: {
      isKerwinRequired: true
    }
  },
  {
    path: '/login',
    component: Login
  },
  // 重定向
  {
    path: '*',
    redirect: 'films'
  }
]

const router = new VueRouter({
  mode: 'history',
  routes
})

// 全局拦截
// router.beforeEach((to, from, next) => {
//   // console.log(to)
//   if (to.meta.isKerwinRequired) {
//     if (localStorage.getItem('token')) {
//       next()
//     } else {
//       next({
//         path: '/login',
//         query: { aaa: to.fullPath}
//       })
//     }
//   } else {
//     next()
//   }

// })

export default router
